package hzh;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

public class EditMoviesServlet extends HttpServlet {
    private static final long serialVersionUID = 119876388866686380L;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String sql = "SELECT * FROM movies WHERE id=" + id;
        try {
            List<Movies> movies = DBUtils.getMovies(sql);
            if (movies.size() == 0) {
                response.sendRedirect("./list1.jsp");
                return;
            }
            Movies movie = movies.get(0);
            StringBuilder sb = new StringBuilder();
            sb.append(PageUtils.getAdminHeader());
            sb.append(buildMovieForm(movie));
            sb.append(PageUtils.getEnd());
            response.setCharacterEncoding("UTF-8");
            try (Writer writer = response.getWriter()) {
                writer.write(sb.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String buildMovieForm(Movies movie) {
        String html = String.format("<center>\n" +
                        "<br>\n" +
                        "<form action='./UpdateServlet' method='POST''>\n" +
                        "<input type='hidden' name='id' value='%s'>" +
                        "<table style='background-color:#dddddd;padding:2em'>\n" +
                        "\t<tr><td>电影名:</td><td><input type='text' name='name' value='%s'></td></tr>\n" +
                        "\t<tr><td>演员:</td><td><input type='text' name='actors'  value='%s'></td></tr>\n" +
                        "\t<tr><td>评分:</td><td><input type='text' name='score'  value='%s'></td></tr>\n" +
                        "\t<tr><td>票价: </td><td><input type='text' name='price'  value='%s'></td></tr>\n" +
                        "\t<tr><td>类别: </td><td>\n" +
                        "\t\t<select name='type'>\n" +
                        "\t\t\t<option value='adventure'>冒险\n" +
                        "\t\t\t<option value='fiction'>科幻\n" +
                        "\t\t\t<option value='love'>爱情\n" +
                        "\t\t\t<option value='comedy'>喜剧\n" +
                        "\t\t\t<option value='cartoon'>动画\n" +
                        "\t\t\t<option value='suspense'>悬疑\n" +
                        "\t\t</select>\t\n" +
                        "\t</td></tr>\n" +
                        "\t<tr><td>描述: </td><td><textarea name='des' cols='50' rows='5'>%s</textarea></td></tr>\n" +
                        "\t<tr><td colspan='2' align='center'><br><input type='submit' value='提交修改'></td></tr>\n" +
                        "</table>\t\n" +
                        "</form>\n" +
                        "</div>\n" +
                        "</center>",
                movie.getId(), movie.getName(), movie.getActors(), movie.getScore(),movie.getPrice(), movie.getDescribe()
        );
        html = html.replace("'" + movie.getType() + "'", "'" + movie.getType() + " 'selected");
        return html;
    }
}
